package com.liu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.dao.AdminDao;
import com.liu.entity.Admin;
import com.liu.entity.User;
import com.liu.service.intf.AdminService;
import com.liu.vo.PageBean;
import com.liu.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao dao;
    @Override
    public List<Admin> select(Admin admin) {
        List<Admin> users=dao.selectByName(admin.getUsername());
        return users;

    }

    @Override
    public R save(Admin admin) {
        //执行新增操作
        if(dao.add(admin)>0){
            //成功
            return R.ok();
        }else{
            //失败
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Admin> pageInfo=new PageInfo<>(dao.selectAll());
        //3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R del(int id) {
        //1.验证参数
        if(id>0){
            //2.操作数据库 验证是否成功
            if(dao.del(id)>0){
                //删除成功
                return R.ok();
            }else{
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }

    @Override
    public R update(Admin admin) {
        if(dao.update(admin)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public PageBean queryPageWhere(String username, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(username)){
            username=null;
        }else{
            username="%"+username+"%";//包含  模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Admin> pageInfo=new PageInfo<>(dao.selectWhere(username));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
    @Override
    public R login(Admin admin) {
        List<Admin> users = select(admin);
        System.out.println(users);
        if (users.size() != 0){
            if (users.get(0).getPassword().equals(admin.getPassword())){
                return new R(200,"登录成功",null);
            }else{
                return new R(400,"密码错误",null);
            }
        }else {
            return new R(300,"用户名不存在",null);
        }

    }

    @Override
    public R login(Admin admin, HttpSession session) {
        return null;
    }
}
